#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

int daysInMonth[12] = { 31, 28, 31, 30, 
                        31, 30, 31, 31, 
                        30, 31, 30, 31};

#define isRun(y) ( (y)%100==0 ? ((y)%400 == 0) : ((y)%4 == 0) )
#define daysInM(y,m,d) ( m==2 ? (isRun(y)?29:28): (daysInMonth[(m)-1]))
#define dateValid(y,m,d) (m>=1 && m<=12 && d>=1 && d <= daysInM(y,m,d))
#define AgtB(Ay, Am, Ad, By, Bm, Bd) (\
            Ay > By ? Ay > By : ( Ay < By ? Ay < By : (\
            Am > Bm ? Am > Bm : (Am < Bm ? Am < Bm : ( Ad > Bd)))))

int main(int argc, char **argv) {
    if(argc != 3) {
        printf("argc != 3 !! \n");
        printf("usage: %s YYYYMMDD YYYYMMDD\n", argv[0]);
        return -1;
    } else if(strlen(argv[1]) != 8 || strlen(argv[2]) != 8 ) {
        printf("usage: %s YYYYMMDD YYYYMMDD\n", argv[0]);
        return -1;
    }

    char y_begin[5], y_end[5];
    char m_begin[3], m_end[3];
    char d_begin[3], d_end[3];

    int iYBegin, iMBegin, iDBegin, iYEnd, iMEnd, iDEnd;

    memcpy(y_begin, argv[1], 4);        y_begin[4]='\0';    iYBegin = atoi(y_begin);
    memcpy(m_begin, argv[1]+4, 2);      m_begin[2]='\0';    iMBegin = atoi(m_begin);
    memcpy(d_begin, argv[1]+6, 2);      d_begin[2]='\0';    iDBegin = atoi(d_begin);

    memcpy(y_end, argv[2], 4);          y_end[4]='\0';      iYEnd= atoi(y_end);
    memcpy(m_end, argv[2]+4, 2);        m_end[2]='\0';      iMEnd= atoi(m_end);
    memcpy(d_end, argv[2]+6, 2);        d_end[2]='\0';      iDEnd= atoi(d_end);

    if((!dateValid(iYBegin, iMBegin, iDBegin)) || (!dateValid(iYEnd, iMEnd, iDEnd))) {
        printf("invalid date ...\n");
        return -1;
    }

    if(AgtB(iYBegin, iMBegin, iDBegin, iYEnd, iMEnd, iDEnd)) {
        printf("usage: %s YYYYMMDD YYYYMMDD(start, end)\n", argv[0]);
        return -1;
    }

    int days_count = 0;
    int iY, iM, iD;
    for(iY = iYBegin, iM = iMBegin, iD = iDBegin; ; iD ++, days_count ++) {
        if(iD > daysInM(iY, iM, iD) ) {
            iD = 1;
            if(iM != 12) {
                iM ++;
            } else {
                iM = 1;
                iY ++;
            }
        }
        //printf("%d/%d/%d\n", iY, iM, iD);
        if (iY == iYEnd && iM == iMEnd && iD == iDEnd) {
            break;
        }
    }

    printf("day count from %d/%d/%d to %d/%d/%d = %d\n", 
            iYBegin,iMBegin,iDBegin, iYEnd,iMEnd,iDEnd, days_count);
    
    return 0;
}
